package cn.bluethink.eguan.core.service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.bluethink.eguan.core.entity.EgFileEntity;
import cn.bluethink.eguan.core.mapper.EgFileMapper;
import cn.bluethink.eguan.model.core.EgFile;
import cn.bluethink.eguan.model.core.EgOType;
import cn.bluethink.eguan.utils.IdMakerUtils;
import tk.mybatis.mapper.entity.Example;

/**
 * 文件业务层
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.wanglei于2018/12/20设计并构建初始版本v0.3.0
 */
@Service
public class EgFileService {

    @Autowired
    private EgFileMapper fileMapper;

    private IdMakerUtils idMakerUtil = new IdMakerUtils();

    /**
     * 批量提交文件
     * 
     * @param fileList
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public List<EgFile> createList(List<EgFile> fileList) throws Exception {
        List<EgFileEntity> entities = new ArrayList<>();
        for (EgFile file : fileList) {
            file.setId(idMakerUtil.nextId());
            EgFileEntity entity = new EgFileEntity(file);
            // 设置文件的类型
            file.setType(new EgOType(EgOType.FILE, null));
            file.setCtime(entity.getCtime());
            entities.add(entity);
        }
        int insertCount = fileMapper.insertList(entities);
        return insertCount > 0 ? fileList : null;
    }

    /**
     * 根据主键删除文件
     * 
     * @param fileId
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public Boolean del(Long fileId) throws Exception {
        int delCount = fileMapper.deleteByPrimaryKey(fileId);
        return delCount > 0 ? true : false;
    }

    /**
     * 根据主键列表删除文件
     * 
     * @param fileIds
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public Boolean delByIds(List<Long> fileIds) throws Exception {
        Example example = new Example(EgFileEntity.class);
        example.and().andIn("id", fileIds);
        int delCount = fileMapper.deleteByExample(example);
        return delCount > 0 ? true : false;
    }

    /**
     * 根据文件主键标识列表查询
     * 
     * @param fileIds
     * @return
     * @throws Exception
     */
    public List<EgFile> queryByFileIds(List<Long> fileIds) throws Exception {
        Example example = new Example(EgFileEntity.class);
        example.and().andIn("id", fileIds);
        List<EgFileEntity> entities = fileMapper.selectByExample(example);
        List<EgFile> result = new ArrayList<>();
        if (entities != null && entities.size() > 0) {
            result = entities.stream().map(e -> e.createEgFile()).collect(Collectors.toList());
        }
        return result;
    }
    
    /**
     * 根据主键获取文件详情
     * 
     * @param fileId
     * @return
     * @throws Exception
     */
    public EgFile detail(Long fileId) throws Exception {
        EgFileEntity entity = fileMapper.selectByPrimaryKey(fileId);
        return entity != null ? entity.createEgFile() : null;
    }

    /**
     * 根据文件所上传的位置对象类型及对象列表查询对应的文件数目
     * 
     * @param posOtype
     * @param posOids
     * @return
     * @throws Exception
     */
    public Integer queryByOtypeAndPos(Integer posOtype, List<Long> posOids) throws Exception {
        Example example = new Example(EgFileEntity.class);
        example.and().andEqualTo("posOtype", posOtype).andIn("posOid", posOids);
        return fileMapper.selectCountByExample(example);
    }

    /**
     * 根据文件所上传的位置对象类型及对象列表删除对应的文件
     * 
     * @param posOtype
     * @param posOids
     * @return
     * @throws Exception
     */
    @Transactional(transactionManager = "eguanTransactionManager", rollbackFor = Exception.class)
    public Boolean delByOtypeAndPos(Integer posOtype, List<Long> posOids) throws Exception {
        Example example = new Example(EgFileEntity.class);
        example.and().andEqualTo("posOtype", posOtype).andIn("posOid", posOids);
        int delCount = fileMapper.deleteByExample(example);
        return delCount > 0 ? true : false;
    }

    /**
     * 根据文件所上传的位置对象及对象类型，上传者列表 获取对应的文件列表
     * 
     * @param posOid
     * @param posOtype
     * @param uploaderIds
     * @return
     * @throws Exception
     */
    public List<EgFile> queryByUploaders(Long posOid, Integer posOtype, List<Long> uploaderIds) throws Exception {
        Example example = new Example(EgFileEntity.class);
        example.and().andEqualTo("posOid", posOid).andEqualTo("posOtype", posOtype).andIn("uploaderId", uploaderIds);
        List<EgFileEntity> entities = fileMapper.selectByExample(example);
        List<EgFile> result = new ArrayList<>();
        if (entities != null && entities.size() > 0) {
            result = entities.stream().map(e -> e.createEgFile()).collect(Collectors.toList());
        }
        return result;
    }

}
